<template>
  <div class="custom-hetong-page">
    <el-tabs
      v-model="searchForm.status"
      class="demo-tabs"
      @tab-click="cTypeChange"
    >
      <el-tab-pane
        v-for="item in setupList"
        :label="item.name"
        :name="item.id"
      ></el-tab-pane>
    </el-tabs>
    <div class="tab4-title">
      <div class="left">
        <el-form inline>
          <el-form-item>
            <el-input
              style="width: 100%"
              v-model="searchForm.name"
              placeholder="合同名称"
            />
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchForm.order_no" placeholder="合同编号" />
          </el-form-item>
          <el-form-item>
            <el-select
              style="width: 100%"
              v-model="searchForm.order_status"
              clearable
              placeholder="合同发货状态"
            >
              <el-option label="待发货" :value="3" />
              <el-option label="已发货" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              style="width: 100%"
              v-model="searchForm.customer_name"
              placeholder="客户名称"
            />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="searchForm.start_date"
              style="width: 100%"
              type="datetime"
              placeholder="开始时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="searchForm.end_date"
              style="width: 100%"
              type="datetime"
              placeholder="结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>

          <el-form-item style="width: 240px !important">
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
            <el-button type="success" @click="downFun"
              ><el-icon> <IEpDownload /> </el-icon>导出</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-button class="right" type="primary" @click="addFun">
        + 添加
      </el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="margin-top: 10px"
      :height="store.tableHeight - 160"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="order_no" label="合同编号" width="200" />
      <el-table-column prop="order_name" label="合同名称" width="150" />
      <el-table-column prop="customer_name" label="客户名称" width="150" />
      <el-table-column prop="amount_real" label="合同金额(元)" width="130" />
      <el-table-column prop="order_date" label="签署日期" width="170" />
      <el-table-column prop="pay_amount" label="已收款" />
      <el-table-column prop="" label="发货状态">
        <template #default="{ row }">
          <el-tag v-if="row.order_status == 3">待发货</el-tag>
          <el-tag v-else type="success">已发货</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="a" label="合同状态" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.status == 1">实施中</el-tag>
          <el-tag v-else-if="row.status == 2" type="warning">已完结</el-tag>
          <el-tag v-else-if="row.status == 3" type="success">已付全款</el-tag>
          <el-tag v-else-if="row.status == 0" type="danger">待开始</el-tag>
          <el-tag v-else-if="row.status == -1" type="info">无效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="owner" label="销售" />
      <el-table-column prop="created_at" label="录入时间" width="180" />
      <el-table-column width="120" label="操作" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link @click="saveFun(row)">
            编辑
          </el-button>
          <el-button type="danger" link @click="delFun(row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-content">
      <div class="left">
        <!-- <div style="margin-right: 20px">
          <label>合同总数：</label>
          <span> {{ statisticsInfo.num }} </span>
        </div> -->
        <div>
          <label>合同总金额：</label>
          <span> ￥{{ statisticsInfo.parice }} </span>
        </div>
      </div>
      <el-pagination
        v-model:currentPage="currentPage"
        v-model:page-size="pageSize"
        :page-="[20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @sisizesze-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <div></div>
    </div>
  </div>
  <addDialog
    :dataInfo="customerInfo"
    ref="addDialogRef"
    @refreshList="refreshList"
  />
  <kehuPage ref="kehuPageRef" @selectCustomer="selectCustomer" />
</template>
<script setup>
import {
  all_listApi,
  delApi,
  exportApi,
} from "@/plugins/api/customer/manage/tab4";
import { useCounterStore } from "@/stores/counter.js";
import addDialog from "../customer/manage/components/tabDialog/Tab4-dialog.vue";
// 客户
import kehuPage from "./components/Kehu.vue";
// 合同信息

const store = useCounterStore();
//
let setupList = [
  {
    name: "全部",
    id: -2,
  },
  {
    name: "无效",
    id: -1,
  },
  {
    name: "待开始",
    id: 0,
  },
  {
    name: "实施中",
    id: 1,
  },
  {
    name: "已完结",
    id: 2,
  },
  {
    name: "已付全款",
    id: 3,
  },
];
function cTypeChange(data) {
  searchForm.value.status = data.props.name;
  currentPage.value = 1;
  getData();
}
//
let searchForm = ref({
  status: -2,
});
function searchFun() {
  currentPage.value = 1;
  getData();
}
function resetFun() {
  searchForm.value = {};
  searchForm.value.status = -2;
  currentPage.value = 1;
  getData();
}
//

let tableData = ref([]);
// 分页
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getData();
};
let statisticsInfo = ref({
  num: 0,
  parice: 0,
});
function getData() {
  let obj = {
    ...{
      page: currentPage.value,
      pageSize: pageSize.value,
    },
    ...searchForm.value,
  };

  if (obj.status == -2) {
    obj.status = "";
  }
  all_listApi(obj).then((res) => {
    tableData.value = res.list;
    statisticsInfo.value.num = res.total;
    statisticsInfo.value.parice = res.total_money;
    total.value = res.total;
  });
}
function refreshList() {
  getData();
}
let kehuPageRef = ref(null);
let addDialogRef = ref(null);
function addFun() {
  kehuPageRef.value.initFun();
}
function saveFun(row) {
  customerInfo.value = { ...row };
  setTimeout(() => {
    addDialogRef.value.initFun("update", row);
  });
}

function delFun(row) {
  ElMessageBox.confirm("确认批量删除选中的数据，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id,
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });

        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
// 选中客户
let customerInfo = ref({});
function selectCustomer(row) {
  customerInfo.value = { ...row };
  setTimeout(() => {
    addDialogRef.value.initFun("add");
  });
}
// 导出
function downFun() {
  let obj = { ...searchForm.value };

  if (obj.status == -2) {
    obj.status = "";
  }
  exportApi(obj).then((res) => {
    const url = window.URL.createObjectURL(new Blob([res]));
    const link = document.createElement("a");
    link.href = url;
    link.setAttribute("download", "合同.xlsx");
    document.body.appendChild(link);
    link.click();
  });
}
//
onMounted(() => {
  getData();
});
</script>

<style lang="scss" scoped>
.custom-hetong-page {
  width: 100%;

  .tab4-title {
    display: flex;
    justify-content: space-between;

    background: #fafbfc;
    box-sizing: border-box;
    padding: 10px;

    .left {
      width: calc(100% - 80px);

      :deep(.el-form-item) {
        width: 20% !important;
        margin-bottom: 10px;
      }
    }

    .right {
      margin-right: 10px;
    }
  }

  .pagination-content {
    display: flex;
    justify-content: space-between;
    margin-top: 8px;

    .left {
      align-self: center;
      margin-right: 20px;
      display: flex;

      label {
        font-size: 14px;
        font-weight: 500;
        color: #5e6c84;
      }

      span {
        font-size: 14px;
        font-weight: 500;
        color: #005ecc;
      }
    }
  }
}
</style>
